{% extends 'admin/model/list.html' %}
{% import 'admin/model/layout.html' as model_layout with context %}

{% block model_menu_bar %}
{% endblock %}

{% block model_list_table %}
    <div class="box box-primary">
        <!--重写表头-->
        <div class="box-header with-border">
            <div class="box-title">
                <h4> {{ _gettext('List') }}{% if count %} ({{ count }}){% endif %}</h4>

            </div>
            <div class="box-tools">
                {% if search_supported %}
                    {#                    {{ model_layout.search_form(input_class='span2 btn-title') }}#}
                    <form method="GET" action="{{ return_url }}" class="navbar-form navbar-left" role="search">
                        {% if sort_column is not none %}
                            <input type="hidden" name="sort" value="{{ sort_column }}">
                        {% endif %}
                        {% if sort_desc %}
                            <input type="hidden" name="desc" value="{{ sort_desc }}">
                        {% endif %}
                        {% if search %}
                            <div class="input-group">
                                <input type="text" name="search" value="{{ search }}"
                                       class="span2 btn-title form-control" placeholder="{{ _gettext('Search') }}">
                                <a href="{{ clear_search_url }}" class="input-group-addon clear"><span
                                        class="fa fa-times glyphicon glyphicon-remove"></span></a>
                            </div>
                        {% else %}
                            <div class="form-group">
                                <input type="text" name="search" value="" class="span2 btn-title form-control"
                                       placeholder="{{ _gettext('Search') }}">
                            </div>
                        {% endif %}
                    </form>
                {% endif %}

                {% if actions %}
                    <div class="btn-group btn-menu">
                        {{ actionlib.dropdown(actions, btn_class='btn dropdown-toggle btn-title btn-success ') }}
                    </div>
                {% endif %}

                {% block model_menu_bar_before_filters %}{% endblock %}

                {% if filters %}
                    <div class="btn-group btn-menu">
                        {{ model_layout.filter_options(btn_class='btn dropdown-toggle btn-title btn-success') }}
                    </div>
                {% endif %}

                {% if admin_view.can_export %}
                    <!--重写导出，修复btn_class遗漏和多一层li-->
                    <div class="btn-group btn-menu">
                        {% if admin_view.export_types|length > 1 %}
                            <a class="btn dropdown-toggle btn-title btn-success" data-toggle="dropdown"
                               href="javascript:void(0)">
                                {{ _gettext('Export') }}<b class="caret"></b>
                            </a>
                            <ul class="dropdown-menu field-filters">
                                {% for export_type in admin_view.export_types %}
                                    <li>
                                        <a href="{{ get_url('.export', export_type=export_type, **request.args) }}"
                                           title="{{ _gettext('Export') }}">{{ _gettext('Export') + ' ' + export_type|upper }}</a>
                                    </li>
                                {% endfor %}
                            </ul>
                        {% else %}
                            <a href="{{ get_url('.export', export_type=admin_view.export_types[0], **request.args) }}"
                               title="{{ _gettext('Export') }}"
                               class="btn dropdown-toggle btn-title btn-success">{{ _gettext('Export') }}</a>
                        {% endif %}
                        {#                        {{ model_layout.export_options(btn_class='btn dropdown-toggle btn-title btn-success') }}#}
                    </div>
                {% endif %}

                {% if can_set_page_size %}
                    <div class="btn-group btn-menu">
                        {{ model_layout.page_size_form(page_size_url,btn_class='btn dropdown-toggle btn-title btn-default') }}
                    </div>
                {% endif %}

                {% if admin_view.can_create %}
                    {% if admin_view.create_modal %}
                        {{ lib.add_modal_button(url=get_url('.create_view', url=return_url, modal=True), title=_gettext('Create New Record'), content=_gettext('Create')) }}
                    {% else %}
                        <div class="btn-group btn-menu">
                            <a href="{{ get_url('.create_view', url=return_url) }}" class="btn btn-danger"
                               title="{{ _gettext('Create New Record') }}">{{ _gettext('Create') }}</a>
                        </div>
                    {% endif %}
                {% endif %}

                {% block model_menu_bar_after_filters %}{% endblock %}
            </div>

        </div>

        <!--重写列表-->
        <div class="box-body">
            <div class="table-responsive">
                <!--故意固定列宽：table里style="table-layout: fixed;word-break:break-all;"，th里style="width:120px"-->
                <table class="table table-bordered model-list" style="table-layout: fixed;word-break:break-all;">
                    <thead>
                    <tr>
                        {% block list_header scoped %}
                            {% if actions %}
                                <th class="list-checkbox-column" style="width:20px">
                                    <input type="checkbox" name="rowtoggle" class="action-rowtoggle"
                                           title="{{ _gettext('Select all records') }}"/>
                                </th>
                            {% endif %}
                            {% block list_row_actions_header %}
                                {% if admin_view.column_display_actions %}
                                    <th class="col-md-1" style="width:60px">&nbsp;</th>
                                {% endif %}
                            {% endblock %}
                            {% set column = 0 %}
                            {% for c, name in list_columns %}
                                <th class="column-header col-{{ c }}" style="width:120px">
                                    {% if admin_view.is_sortable(c) %}
                                        {% if sort_column == column %}
                                            <a href="{{ sort_url(column, True) }}"
                                               title="{{ _gettext('Sort by %(name)s', name=name) }}">
                                                {{ name }}
                                                {% if sort_desc %}
                                                    <span class="fa fa-chevron-up glyphicon glyphicon-chevron-up"></span>
                                                {% else %}
                                                    <span class="fa fa-chevron-down glyphicon glyphicon-chevron-down"></span>
                                                {% endif %}
                                            </a>
                                        {% else %}
                                            <a href="{{ sort_url(column) }}"
                                               title="{{ _gettext('Sort by %(name)s', name=name) }}">{{ name }}</a>
                                        {% endif %}
                                    {% else %}
                                        {{ name }}
                                    {% endif %}
                                    {% if admin_view.column_descriptions.get(c) %}
                                        <a class="fa fa-question-circle glyphicon glyphicon-question-sign"
                                           title="{{ admin_view.column_descriptions[c] }}"
                                           href="javascript:void(0)" data-role="tooltip"
                                        ></a>
                                    {% endif %}
                                </th>
                                {% set column = column + 1 %}
                            {% endfor %}

                        {% endblock %}
                    </tr>
                    </thead>
                    {% for row in data %}
                        <tr>
                            {% block list_row scoped %}
                                {% if actions %}
                                    <td>
                                        <input type="checkbox" name="rowid" class="action-checkbox"
                                               value="{{ get_pk_value(row) }}"
                                               title="{{ _gettext('Select record') }}"/>
                                    </td>
                                {% endif %}
                                {% block list_row_actions_column scoped %}
                                    {% if admin_view.column_display_actions %}
                                        <td class="list-buttons-column">
                                            {% block list_row_actions scoped %}
                                                {% for action in list_row_actions %}
                                                    {{ action.render_ctx(get_pk_value(row), row) }}
                                                {% endfor %}
                                            {% endblock %}

                                            {% if actions|length>1 %}
                                                <!--增加自定义的action列-->
                                                {% for p in actions %}
                                                    {% if p[0]!='delete' %}
                                                        <form class="icon" method="POST"
                                                              action="{{ get_url('.action_view') }}">
                                                            <input type="hidden" name="action" value="{{ p[0] }}">
                                                            <input type="hidden" name="rowid"
                                                                   value="{{ get_pk_value(row) }}">
                                                            <button
                                                                    onclick="return confirm('{{ _gettext(actions_confirmation[p[0]]) }}');">
                                                                <span class="label label-info">{{ _gettext(p[1]) }}</span>
                                                            </button>
                                                        </form>
                                                    {% endif %}
                                                {% endfor %}
                                            {% endif %}
                                        </td>
                                    {%- endif -%}
                                {% endblock %}

                                {% for c, name in list_columns %}
                                    <td class="col-{{ c }}">
                                        {% if admin_view.is_editable(c) %}
                                            {% set form = list_forms[get_pk_value(row)] %}
                                            {% if form.csrf_token %}
                                                {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c), csrf=form.csrf_token._value()) }}
                                            {% else %}
                                                {{ form[c](pk=get_pk_value(row), display_value=get_value(row, c)) }}
                                            {% endif %}
                                        {% else %}
                                            {% set val=get_value(row, c) %}
{#                                            {% if val|length>50 %}#}
{#                                                {{ val[0:50] }} ......#}
{#                                            {% else %}#}
                                                {{ val }}
{#                                            {% endif %}#}
                                        {% endif %}
                                    </td>
                                {% endfor %}


                            {% endblock %}
                        </tr>
                    {% else %}
                        <tr>
                            <td colspan="999">
                                {% block empty_list_message %}
                                    <div class="text-center">
                                        {{ admin_view.get_empty_list_message() }}
                                    </div>
                                {% endblock %}
                            </td>
                        </tr>
                    {% endfor %}
                </table>
            </div>
        </div>
        <div class="box-footer">
            {% block list_pager %}
                {% if num_pages is not none %}
                    {{ lib.pager(page, num_pages, pager_url) }}
                {% else %}
                    {{ lib.simple_pager(page, data|length == page_size, pager_url) }}
                {% endif %}
            {% endblock %}
        </div>
    </div>
{% endblock %}

